<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备sn号" prop="snCode" label-width="72px">
          <el-input
            v-model="queryParams.snCode"
            placeholder="请输入设备sn号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="测试项" prop="testItem">
          <el-input
            v-model="queryParams.testItem"
            placeholder="请输入测试项"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="测试结果" prop="testResult">
          <el-select 
            v-model="queryParams.testResult" 
            placeholder="请选择" 
            clearable
          >
            <el-option 
            v-for="item in testResult_list" 
            :key="item.value" 
            :label="item.label"
            :value="item.value" 
            />
          </el-select>
          <!-- <el-input
            v-model="queryParams.testResult"
            placeholder="请输入测试结果"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['produce:product-test-info:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['produce:product-test-info:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['produce:product-test-info:removed']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['produce:product-test-info:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="ProductTestInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="设备sn号" align="center" prop="snCode" />
        <el-table-column label="测试项" align="center" prop="testItem" />
        <el-table-column label="测试内容" align="center" prop="testContent">
            <template slot-scope="scope">
              <div v-if="scope.row.testItem == '抓图上传' && scope.row.testContent">
                <el-image 
                  style="width: 100px; height: 100px"
                  :src="scope.row.testContent.split(',')[0]" 
                  :preview-src-list="scope.row.testContent.split(',').slice(0,scope.row.testContent.split(',').length-1)">
                </el-image>
              </div>
              <div v-else-if="scope.row.testItem == '方向'">{{ scope.row.testContent == '0' ? '逆时针' : '顺时针' }}</div>
              <div v-else-if="scope.row.testItem == '角度'">{{ scope.row.testContent + '度' }}</div>
              <div v-else-if="scope.row.testItem == '亮度'">{{ scope.row.testContent + '%' }}</div>
              <div v-else-if="scope.row.testItem == '曝光'">{{ scope.row.testContent + '(ISO)' }}</div>
              <div v-else>{{ scope.row.testContent }}</div>
            </template>
        </el-table-column>
        <el-table-column label="测试结果" align="center" prop="testResult">
          <template slot-scope="scope">
            {{ scope.row.testResult === 0 ? '通过' : '不通过' }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleDetail(scope.row)"
              v-hasPermi="['produce:product-test-info:edit']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['produce:product-test-info:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['produce:product-test-info:removed']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改产品测试详情对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="设备sn号" prop="snCode">
            <el-input v-model="form.snCode" placeholder="请输入设备sn号" />
          </el-form-item>
          <el-form-item label="测试项" prop="testItem">
            <el-input v-model="form.testItem" placeholder="请输入测试项" />
          </el-form-item>
          <el-form-item label="测试内容">
            <editor v-model="form.testContent" :min-height="192"/>
          </el-form-item>
          <el-form-item label="测试结果" prop="testResult">
            <!-- <el-input v-model="form.testResult" placeholder="请输入测试结果" /> -->
            <el-select 
              v-model="form.testResult" 
              placeholder="请选择" 
              clearable
            >
              <el-option 
              v-for="item in testResult_list" 
              :key="item.value" 
              :label="item.label"
              :value="item.value" 
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listProductTestInfo, getProductTestInfo, removedProductTestInfo, addProductTestInfo, updateProductTestInfo } from "@/api/produce/product-test-info";
  import { getProductInfoBySn } from "@/api/produce/product-sn-info";
  export default {
    name: "Product-test-info",
    data() {
      return {
        // 测试结果
        testResult_list:[
          {value:0,label:'通过'},
          {value:1,label:'不通过'}
        ],
        // 产品详情
        detailDialogOpen: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 产品测试详情表格数据
        ProductTestInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          snCode: null,
          testItem: null,
          testContent: null,
          testResult: null,
          removed: null,
          orderByColumn:'id',
          isAsc:'desc'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          snCode: [
            { required: true, message: "设备sn号不能为空", trigger: "blur" }
          ],
          testItem: [
            { required: true, message: "测试项不能为空", trigger: "blur" }
          ],
          testContent: [
            { required: true, message: "测试内容不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      let str = "https://genius-prod.oss-cn-beijing.aliyuncs.com/PICTURE/20231123/d5842c9e872d4036b2398e08fe976ed22023-11-23-19-23-06.jpg,https://genius-prod.oss-cn-beijing.aliyuncs.com/PICTURE/20231123/f48df7533c9b48fbb047563b03012db92023-11-23-19-23-26.jpg,"
      console.log(str.split(','))
    },
    methods: {
      /** 查询产品测试详情列表 */
      getList() {
        this.loading = true;
        listProductTestInfo(this.queryParams).then(response => {
          this.ProductTestInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          snCode: null,
          testItem: null,
          testContent: null,
          testResult: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          removed: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加产品测试详情";
      },
      /** 详情按钮操作 */
      handleDetail(row){
        console.log(row)
        const param = {
          snCode : row.snCode
        }
        //详情接口调用
        console.log("传参为",param)
        getProductInfoBySn(param).then(response => {
          console.log("根据sn获取产品详情",response)
          //详情弹出框打开
          this.detailDialogOpen = true
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getProductTestInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改产品测试详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateProductTestInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProductTestInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除产品测试详情编号为"' + ids + '"的数据项？').then(function() {
            // 修改为逻辑删除
          return removedProductTestInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('produce/product-test-info/export', {
          ...this.queryParams
        }, `product-test-info_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  